package com.mygame.game_server.cmdHandle;

import com.mygame.game_server.User;
import com.mygame.game_server.caster.Broadcaster;
import com.mygame.game_server.manager.UserManager;
import com.mygame.game_server.msg.GameMsgProtocol;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;

/**
 * @author Jim
 * @date 22:58 2022/5/6
 * @description
 * @email:huwmz04153277@126.com
 */
public class UserEntryCmdHandler implements ICmdHandler<GameMsgProtocol.UserEntryCmd> {

    @Override
    public void handle(ChannelHandlerContext ctx, GameMsgProtocol.UserEntryCmd cmd) {
        if (null == ctx || cmd == null) {
            return;
        }
        // 从会话中获取信息
        Integer userId = (Integer) ctx.channel().attr(AttributeKey.valueOf("userId")).get();
        if (userId == null) {
            return;
        }
        // String heroAvatar = (String) ctx.channel().attr(AttributeKey.valueOf("heroAvatar")).get();
        User existUser = UserManager.getUserById(userId);
        GameMsgProtocol.UserEntryResult.Builder resultBuilder = GameMsgProtocol.UserEntryResult.newBuilder();
        resultBuilder.setUserId(userId);
        resultBuilder.setHeroAvatar(existUser.heroAvatar);
//        resultBuilder.setUserName(existUser.userName);

        // 构建结果并广播
        GameMsgProtocol.UserEntryResult newResult = resultBuilder.build();
        Broadcaster.broadcaster(newResult);
    }


}
